﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace System.Web
{
    /// <summary>
    /// 上传文件信息对象
    /// </summary>
    public class UploadFileInfo
    {
        /// <summary>
        /// 当前请求上线文
        /// </summary>
        private HttpContext _Context = HttpContext.Current;
        /// <summary>
        /// 判断当前上传是否使用绝对保存地址
        /// </summary>
        public bool IsUseAbsolute { get; set; }
        /// <summary>
        /// 是否重命名保存文件
        /// </summary>
        public bool IsRename { get; set; }
        /// <summary>
        /// 当前上传文件的字节大小
        /// </summary>
        public int ContentLength { get; set; }
        /// <summary>
        /// 新文件名
        /// </summary>
        public string NewName { get; set; }
        /// <summary>
        /// 原文件名
        /// </summary>
        public string OldName { get; set; }
        /// <summary>
        /// 当前上传文件的扩展名
        /// </summary>
        public string Extention
        {
            get
            {
                if (string.IsNullOrEmpty(OldName))
                    return "";
                int index = OldName.LastIndexOf(".");
                if (index < 0)
                    return "";
                string extension = OldName.Substring(index, OldName.Length - index);
                return extension;
            }
        }
        /// <summary>
        /// 临时文件夹下的新文件名
        /// </summary>
        public string NewFullName_Temp
        {
            get { return TempFile + NewName; }
        }
        /// <summary>
        /// 临时文件夹下的就文件名
        /// </summary>
        public string OldFullName_Temp
        {
            get { return TempFile + OldName; }
        }
        /// <summary>
        /// 上传的 临时文件夹
        /// </summary>
        private string _TempFile;
        public string TempFile
        {
            get
            {
                return _TempFile;
            }
            set
            {
                if (!Directory.Exists(value))
                {
                    Directory.CreateDirectory(value);
                }
                _TempFile = value;
            }
        }
        /// <summary>
        /// 上传文档 保存文件夹
        /// </summary>
        private string _UploadFiles;
        /// <summary>
        /// 后台用户上传目录
        /// </summary>
        public string UploadFiles
        {
            get
            {
                return this._UploadFiles;
            }
            set
            {
                if (!Directory.Exists(value))
                {
                    Directory.CreateDirectory(value);
                }
                _UploadFiles = value;
            }
        }
        /// <summary>
        /// 前台用户上传 目录
        /// </summary>
        public string UploadFiles_Above
        {
            get
            {
                return _UploadFiles.Replace("UploadFiles", "UploadAbove");
            }
        }

        /// <summary>
        /// 初始化对象
        /// 默认：需要需要重命名，
        /// 上传的临时文件夹，保存文件夹
        /// </summary>
        public UploadFileInfo()
        {
            Init();
        }
        /// <summary>
        /// 指定HttpPostedFile，初始化对象
        /// </summary>
        /// <param name="postedFile"></param>
        public UploadFileInfo(HttpPostedFile postedFile)
        {
            //原文件名
            OldName = postedFile.FileName;
            //当前文件大小
            ContentLength = postedFile.ContentLength;
            Init();
        }
        /// <summary>
        /// 设置为绝对路径接收
        /// </summary>
        /// <param name="TempFile"></param>
        /// <param name="UploadFiles"></param>
        public void SetPath(string TempFile, string UploadFiles)
        {
            this.IsUseAbsolute = true;
            this.TempFile = TempFile;
            this.UploadFiles = UploadFiles;
        }
        /// <summary>
        /// 初始化设置
        /// </summary>
        private void Init()
        {
            //优先使用WebConfig中的地址
            TempFile = Common.ConfigValue.TempFile;
            UploadFiles = Common.ConfigValue.UploadPath;
            IsRename = true;
            //产生新文件名
            if (IsRename)
            {
                NewName = Guid.NewGuid() + Extention;
            }
        }
        /// <summary>
        /// 产生一个新的文件名
        /// </summary>
        /// <param name="extention"></param>
        /// <returns></returns>
        public string CreateNewName(string extention)
        {
            NewName = Guid.NewGuid().ToString() + extention;
            return NewName;
        }
        /// <summary>
        /// 判断当前文件是否在临时文件夹中存在
        /// </summary>
        /// <returns></returns>
        public bool ExitsFromTempFile(string fileName)
        {
            FileInfo info = new FileInfo(TempFile + fileName);
            if (info == null)
                return false;
            return info.Exists;
        }
        /// <summary>
        /// 获取当前上传文件夹的子文件夹,指定是否使用后台目录，还是前台的目录
        /// </summary>
        /// <param name="subFolder">子文件夹名</param>
        /// <returns></returns>
        public string GetUploadFiles_Sub(string subFolder, bool IsBack)
        {
            DirectoryInfo info = new DirectoryInfo(IsBack ? UploadFiles : UploadFiles_Above);
            if (!info.Exists)
            {
                info.Create();
            }
            return info.CreateSubdirectory(subFolder).FullName;
        }

    }
}